package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Service
public class DtsRolesServiceImpl implements com.qf.service.DtsRolesService {
   @Autowired
    DtsRoleMapper roleMapper;
   @Autowired
   DtsPermissionMapper permissonMapper;
    @Override
    public Set<String> findByRoleIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.in(DtsRole::getId,roleIds).select(DtsRole::getName);
        List<DtsRole>roles=roleMapper.selectList(queryWrapper);
        Set<String>rolesNames=new HashSet<>();
        for (DtsRole role : roles) {
            rolesNames.add(role.getName());
        }
        return rolesNames;
    }

    @Override
    public List<DtsRole> findAll() {
        List<DtsRole> dtsRoles = roleMapper.selectList(null);
        return dtsRoles;
    }

    @Override
    public IPage<DtsRole> selectByPage(Integer page, Integer limit, String name, String sort, String order) {
        IPage<DtsRole>iPage=new Page<>(page,limit);
        QueryWrapper queryWrapper=new QueryWrapper();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            queryWrapper.orderByAsc(sort);
        }else{
            queryWrapper.orderByDesc(sort);
        }

        IPage iPage1 = roleMapper.selectPage(iPage, queryWrapper);
        return iPage1;
    }

    @Override
    public DtsRole insert(DtsRole role) {
        role.setAddTime(new Date());
        role.setDeleted(false);
        role.setUpdateTime(new Date());
        role.setEnabled(true);
        roleMapper.insert(role);

        return role;
    }

    @Override
    public Boolean checkRole(String name) {
        LambdaQueryWrapper<DtsRole> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DtsRole::getName,name);
        Integer integer = roleMapper.selectCount(queryWrapper);
        return integer!=0;
    }

    @Override
    public DtsRole updateById(DtsRole role) {
        role.setUpdateTime(new Date());
        role.setAddTime(new Date());
        role.setDeleted(false);
        role.setEnabled(true);
        roleMapper.updateById(role);
        return role;
    }

    @Override
    public void delete(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public boolean checkIsSuperManager(Integer roleId) {
        DtsRole dtsRole = roleMapper.selectById(roleId);
        if(dtsRole.getName()!=null&&dtsRole.getName().equals("超级管理员")){
            return true;
        }
        return false;
    }

    @Override
    public void UpdatePermissions(Integer roleId, List<String> permissions) {
        //先删除管理员的权限添加新的权限
        //因为roleId不是主键所以不能用deleteById这个方法，要用LambdaQueryWrapper创造条件
        LambdaQueryWrapper<DtsPermission> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DtsPermission::getRoleId,roleId);
        permissonMapper.delete(queryWrapper);
        if(permissions!=null&&permissions.size()>0){
            for (String permission : permissions) {
                DtsPermission perm=new DtsPermission();
                perm.setPermission(permission);
                perm.setAddTime(new Date());
                perm.setDeleted(false);
                perm.setRoleId(roleId);
                perm.setUpdateTime(new Date());
                permissonMapper.insert(perm);
            }

        }
    }


}
